<?php

namespace App\Console\Commands;

use App\Miya\Models\UserView;
use Carbon\Carbon;
use Faker\Provider\Address;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class UserViewData extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'user_views:build';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '虚拟生成访问记录';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $start = now()->addMonths(-3)->format('Y-m-d');

        $n = 10;

        for (
            $day = $start;
            $day <= now()->format('Y-m-d');
            $day = Carbon::parse($day)->addDay()->format('Y-m-d')
        ) {
            $data = [];
            for ($i = 0; $i < $n; $i++) {
                $ip = sprintf('%s.%s.%s.%s', mt_rand(1, 999), mt_rand(1, 999), mt_rand(1, 999), mt_rand(1, 999));
                $data[] = ['ip' => $ip, 'time' => $day, 'day' => $day, 'url' => 'http://miya.test', 'referer_url' => ' '];
            }

            $n += mt_rand(-20, 100);

            DB::table('user_views')->insert($data);
        }
    }
}
